#include "SampleData.h"


#include <cmath> // 添加用于平方根等数学运算
#include <iostream>
#include <vector>
#include <random>
#include <numeric>
#include <algorithm>

namespace sample {


    Metric::Type SampleFrame::getColumnType(const std::string& metricName) const
    {
        auto it = data_.find(metricName);
        if (it == data_.end()) {
            std::cout << "Error: metric name not found" << std::endl;
            return Metric::TypeUnknown;
        }

        return it->first.type;
    }

    double SampleFrame::calculatePopulationStdDev(string& metricName) {
        // 计算标准差
        return sqrt(calculatePopulationVariance(metricName));
    }

    double SampleFrame::calculatePopulationVariance(string& metricName) {
        auto type = getColumnType(metricName);
        if (type != Metric::TypeInt && type != Metric::TypeDouble) {
            return 0.0;
        }
        ColumnData* c = getColumn(metricName);
        if (c == nullptr) {
            return 0.0;
        }

        std::vector<double> data;
        for (auto& d : *c) {
            if (type == Metric::TypeInt) {
                data.push_back(std::get<int>(d));
            }
            else if (type == Metric::TypeDouble) {
                data.push_back(std::get<double>(d));
            }
        }
        //std::vector<double> cd = getColumnData<double>(metricName);

        // 计算平均值
        double mean = std::accumulate(data.begin(), data.end(), 0.0) / data.size();

        //double sampleMean = boost::accumulators::mean(acc);
        //double sampleVariance = boost::accumulators::variance(acc);

        // 计算方差
        return std::accumulate(data.begin(), data.end(), 0.0,
            [&](double acc, double val) { return acc + pow(val - mean, 2); }) / data.size();
    }
}